import Information from "../../model/information";
import Service from "../../model/service"
import { getDataSet, getEventParam } from "../../utils/util";

Page({
  data: {
    meetIndex: null, // 评审邀请按钮展示
    meetingList: [], // 评审邀请
    meetingCount: 0,
    checkList: [], // 项目审核
    checkCount: 0,
    projectList: [], //项目
    show: false, // 拒绝参会
    formData: {
      reason: ''
    },
    rules: [{
      name: 'reason',
      rules: {
        required: true,
        message: '请输入原因'
      },
    }, ],
    error: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getList()
    this.init()
  },
  onShow() {
    this._getList()
    this.init()
  },
  async init() {
    let {
      data
    } = await Service.getHomeList()
    // 评审邀请 // 项目审核
    this.setData({
      meetingList: data.mailMeetingList,
      checkList: data.mailAuditList,
      meetingCount: data.meetingCount,
      checkCount: data.auditCount
    })
  },
  // 获取我的项目
  async _getList() {
    // 发起网络请求，获取服务列表的数据
    const {
      rows
    } = await Service.getProject()
    this.setData({
      projectList: rows
    })
  },
  // 会议操作按钮
  handleShow(e) {
    const index = getDataSet(e, 'index')
    if(index === this.data.meetIndex) {
      this.setData({
        meetIndex: null
      })
    } else {
    this.setData({
        meetIndex: index
      })
    }
  },
  // 处理催办
  async handleUrge(e) {
    let that = this
    const urge = getDataSet(e, 'urge')
    // 1. 修改消息状态
    let res = await Information.updateCheckStatus({
      mailId: urge.id,
      projectId: urge.projectId
    })
    if (res.code === 200) {
      let project = res.data
      // 2. 跳转项目详情列表
      wx.navigateTo({
        url: `/pages/project-detail/project-detail?project_id=${project.projectId}&manager_id=${project.managerId}&dept_type=${project.deptType}&tempParent_id=${project.projectClassification}`,
        events: {
          onResetList: function () {
            wx.showToast({
              title: '已催办',
              duration: 2000
            })
            that.init()
          }
        }
      })
    }
  },
  // 跳转项目详情页面
  handleDetail(e) {
    const project = getDataSet(e, 'project')
    wx.navigateTo({
      url: `/pages/project-detail/project-detail?project_id=${project.id}&manager_id=${project.managerId}&dept_type=${project.deptType}&tempParent_id=${project.projectClassification}`,
    })
  },
  // 同意参会
  async handleSure(e) {
    const {
      id,
      projectId,
      mailType
    } = getDataSet(e, 'meeting')
    let query = {
      mailId: id,
      projectId,
      isParticipants: "1",
      mailType
    }
    await wx.showModal({
      title: '注意',
      content: '是否同意参会',
      complete: async (res) => {
        if (res.confirm) {
          let result = await Information.updateExpertsParticipants(query)
          if (result.code == 200) {
            wx.showToast({
              title: '确认参会',
              duration: 2000
            })
            this._getList()
          } else {
            wx.showModal({
              content: result.msg
            })
          }
        }
      }
    })
  },
  // 拒绝参会
  async handleCancel(e) {
    let {
      id,
      senderId,
      mailSendId,
      projectId,
      mailType
    } = getDataSet(e, 'meeting')
    let query = {
      isParticipants: '0',
      content: '',
      receivers: senderId,
      typeSourceId: mailSendId,
      mailId: id,
      projectId,
      mailType
    }
    this.setData({
      show: true,
      value: '',
      params: query
    })
  },
  // 确认拒绝参会
  handleUpdata() {
    this.selectComponent('#form').validate(async (valid, errors) => {
      if (!valid) {
        const errMsg = errors.map(error => error.message)
        this.setData({
          error: errMsg.join(';')
        })
        return
      }
      this.data.params.content = this.data.value
      let result = await Information.updateExpertsParticipants(this.data.params)
            if (result.code == 200) {
              wx.showToast({
                title: '修改成功',
                duration: 2000
              })
              this._getList()
              this.setData({
                show: false
              })
            } else {
              this.setData({
                show: false
              })
              wx.showModal({
                content: result.msg
              })
            }
    })
  },
  // 拒绝原因 输入
  handleInput(e) {
    console.log(e);
    const value = getEventParam(e, 'value')
    const field = getDataSet(e, 'field')
    this.setData({
      [`formData.${field}`]: value
    })
  }
})